<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_DELIVE.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_INS_SHOP,$login['staff_auth']);

$dao = new Class_M_DELIVE;

$isG4  = FALSE;
if   ($login['staff_auth'] == 'G-4') {
	$isG4 = TRUE;
}

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';
	if( isset($_GET['w']) && $_GET['w'] != '' ){
		$forms['matrix_cd'] = $_GET['w'] ;
		$matrixrow = Class_M_OTHER::get_week_name($forms['matrix_cd'], true);
	} else {
		
		echo "エラーパラメータ" ;
		exit;
	}
	
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$forms  = $dao->get( $_GET['id'] );
		
		$forms["id"] = $_GET['id'];
		//$forms["stop_date"] = Class_PWD::int8_to_date($forms["stop_date"]) ;
		
	}else{
		//$forms['auth'] = 0;
	}
	$forms['week_name'] = $matrixrow['week_name'];
}else{
	$forms = $_POST;
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		if ( $err->clear ){
			if( $forms['id'] == '' ){
				$values["matrix_cd"]   = $forms["matrix_cd"] ;
				$values["delive_cd"]   = $forms["delive_cd"] ;
				$values["delive_name"] = $forms["delive_name"] ;
				$values["ins_co_cd"]   = $forms["ins_co_cd"] ;
			}else {
				$cols  = $dao->get_cols();
				foreach($cols as $kcol => $val) {
					$values[$kcol] = $forms[$kcol] ;
				}
				if ( $values["close_day"] == "" || $values["close_time"] == "" ) {
					$err->clear = false;
					$err_mes['close_day_time'] = '<p class="error">締切日時を入力してください</p>';
				} 
				
			}
			
			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
			}
			$where = sprintf(" matrix_cd = '%s'  AND delive_cd = '%s'", $forms['matrix_cd'], $forms['delive_cd'] );
			if( $forms['id'] == '' ){
				if ( $dao->exits($where) ) {
					$err->clear = false;
					$err_mes['delive_cd'] = '<p class="error">この納品コードは重複しています</p>';
				}
			}else{
				$where = sprintf(" matrix_cd = '%s'  AND delive_cd = '%s'", $forms['matrix_cd'], $forms['delive_cd'] );
				if ( !$dao->exits($where) ) {
					$err->clear = false;
					$err_mes['delive_cd'] = '<p class="error">この納品コードは存在していません</p>';
				}
			}
			
		}

		if( $err->clear ){
	
			if( $forms['id'] == '' ){
				if ($dao->add0($values)) {
					$forms['delive_cd'] = $id;
					$smarty->assign('message', '登録が完了しました。');
					
				} else {
					$smarty->assign('message', '登録が失敗しました。');
				}
			}else{
				
				if ($dao->edit( $forms['id'], $values )) {
					$smarty->assign('message', '更新が完了しました。');
				} else {
					$smarty->assign('message', '更新が失敗しました。');
				}
				$id = $forms['delive_cd'];
			}
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}


if ($phase != 'complete') {
	
}

$ins_co = Class_M_OTHER::get_ins_co_array();
if ($isG4){
	$forms["ins_co_cd"]    = $_SESSION['ADMIN_LOGIN']['STAFF_INS_CO_CD'] ;
	$forms["ins_co_name"]  = $ins_co[$forms["ins_co_cd"]];
	$ins_co =array( $forms["ins_co_cd"] =>$forms["ins_co_name"] );
} else {
	$select = array(''=>PLEASE_SELECT_ARR);
	
	$ins_co = array_merge($select, $ins_co);
}
if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);


$smarty->assign('ins_co', $ins_co );

$close_day = array();
$close_day[''] =PLEASE_SELECT_ARR;
for ($i=1;$i<=7;$i++){
	$close_day[$i] = $i;
}
$smarty->assign('close_day', $close_day );

$close_time= array();
$close_time[''] =PLEASE_SELECT_ARR;
for ($i=0;$i<24;$i++){
	$close_time[$i] = $i;
}
$smarty->assign('close_time', $close_time );

$smarty->assign('isG4', $isG4 );
$smarty->assign('flags', $aoki_mservice_flags );

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mins_delive_form.html');
?>